package com.ant.business.system.mapper;

import com.ant.business.system.entity.Module;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 模块管理持久层
 * @author
 * @description
 * @create 2021-03-22 14:25:56
 **/
@Mapper
public interface ModuleDao extends BaseMapper<Module> {

    /**
     * 根据登录用户查询模块信息
     * @param userId
     * @return
     */
    @Select("SELECT " +
            " module.id, module.name, module.title, module.icon, module.path, module.parent_id, module.delete_flag, module.type, " +
            " module.level, module.create_time, module.creator, module.edit_time, module.editor, module.order_by " +
            " FROM tb_module module " +
            " INNER JOIN tb_role_module roleModule ON module.module_id = roleModule.module_id " +
            " INNER JOIN tb_user_role userRole ON userRole.role_id = roleModule.role_id " +
            " WHERE userRole.user_id = #{userId} AND userRole.unit_id = #{unitId} AND COALESCE(module.delete_flag, 0) = 0 " +
            " GROUP BY module.id, module.name, module.title, module.icon, module.path, module.parent_id, module.delete_flag, module.type, " +
            " module.level, module.create_time, module.creator, module.edit_time, module.editor, module.order_by " +
            " ORDER BY module.order_by ")
    List<Module> getModuleListByUserId(@Param("userId") Integer userId, @Param("unitId") Integer unitId);

    /**
     * 该角色本身有的商户模块
     * @param roleId
     * @param unitId
     * @return
     */
    @Select("SELECT " +
            " module.module_id, module.module_name, module.url, module.parent_id, module.delete_flag, module.type, " +
            " module.level, module.create_time, module.creator, module.edit_time, module.editor " +
            " FROM tb_module module " +
            " INNER JOIN tb_role_module roleModule ON module.module_id = roleModule.module_id " +
            " WHERE roleModule.role_id = #{roleId} AND roleModule.unit_id = #{unitId} AND COALESCE(module.delete_flag, 0) = 0 " +
            " ORDER BY module_id ")
    List<Module> getModuleListByRoleId(Integer roleId, Integer unitId);
}